package cn.itcast.ws.service.impl;

import cn.itcast.export.domain.Export;
import cn.itcast.export.domain.ExportProduct;
import cn.itcast.export.domain.ExportProductExample;
import cn.itcast.export.service.ExportProductService;
import cn.itcast.export.service.ExportService;
import cn.itcast.ws.service.EpService;
import cn.itcast.ws.vo.ExportProductResult;
import cn.itcast.ws.vo.ExportProductVo;
import cn.itcast.ws.vo.ExportResult;
import cn.itcast.ws.vo.ExportVo;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("epService")
@Transactional
public class EpServiceImpl implements EpService {

	@Autowired
	private ExportService exportService;

	@Autowired
	private ExportProductService exportProductService;

	public void setExportService(ExportService exportService) {
		this.exportService = exportService;
	}
	public void setExportProductService(ExportProductService exportProductService) {
		this.exportProductService = exportProductService;
	}

	//海关保运
	public int exportE(ExportVo exportVo) throws Exception {
		Export export = new Export();
		BeanUtils.copyProperties(exportVo,export);
		export.setState(2);
		exportService.save(export);
		for (ExportProductVo epv : exportVo.getProducts()) {
			ExportProduct ep = new ExportProduct();
			BeanUtils.copyProperties(epv,ep);
			ep.setEid(export.getEid());
			exportProductService.save(ep);
		}
		return 0;
	}


	//查询报运结果
	public ExportResult getResult(String exportId) throws Exception {
		Export export = exportService.findByExportId(exportId);
		ExportResult er = new ExportResult();
		er.setExportId(export.getExportId());
		er.setRemark(er.getRemark());
		er.setState(export.getState());


		ExportProductExample example = new ExportProductExample();
		ExportProductExample.Criteria criteria = example.createCriteria();
		criteria.andExportIdEqualTo(exportId);
		List<ExportProduct> list = exportProductService.findAll(example);
		List<ExportProductResult> eprs = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			ExportProduct ep = list.get(i);
			ExportProductResult epr = new ExportProductResult();
			epr.setExportProductId(ep.getExportProductId());
			epr.setTax(10 + (i++) * 0.4);
			eprs.add(epr);
		}
		er.setProducts(eprs);
		return er;
	}
}
